<template>
  <a-modal
    :title="model && model.id > 0 ? '编辑业务' : '新增业务'"
    :width="640"
    :visible="visible"
    :confirmLoading="loading"
    @ok="
      () => {
        $emit('ok')
      }
    "
    @cancel="
      () => {
        $emit('cancel')
      }
    "
  >
    <a-spin :spinning="loading">
      <a-form :form="form" v-bind="formLayout">
        <a-form-item v-if="false" label="id">
          <a-input v-decorator="['id', { initialValue: model?.id }]" />
        </a-form-item>
        <a-form-item label="业务名称">
          <a-input v-decorator="['name', { rules: [{ required: true, message: '请输入业务名称' }] }]" placeholder="请输入业务名称"/>
        </a-form-item>
        <a-form-item label="扣款方式">
          <a-select v-decorator="['dedType', { initialValue:'0' }]" v-model="dedType" placeholder="请选择">
            <a-select-option value="0">不扣款</a-select-option>
            <a-select-option value="1">固定值</a-select-option>
            <a-select-option value="2">按比例</a-select-option>
            <a-select-option value="3">自填</a-select-option>
          </a-select>
        </a-form-item>
        <a-form-item label="扣款值" v-if="dedType === '1' || dedType === '2'">
          <a-input v-decorator="['dedValue', { initialValue: model?.dedValue ,rules: [{ required: true, message: '请输入扣款值' }]}]" placeholder="请输入扣款值"/>
        </a-form-item>
        <a-form-item label="扣款供应商" v-if="dedType === '1' || dedType === '2' || dedType === '3'">
          <a-select v-decorator="['supplyId', { initialValue: model?.supplyId,rules: [{ required: true, message: '请选择' }] }]" placeholder="请选择">
            <a-select-option :value="i.id" v-for="i in supplierOption" :key="i.id">{{ i.name }}</a-select-option>
          </a-select>
        </a-form-item>
        <a-form-item label="提成方式">
          <a-select v-decorator="['propType', { initialValue: '1' }]" placeholder="请选择">
            <a-select-option value="1">固定值</a-select-option>
            <a-select-option value="2">按固定毛利</a-select-option>
          </a-select>
        </a-form-item>
        <a-form-item label="提成值">
          <a-input v-decorator="['propValue', { initialValue:0 }]" placeholder="请输入提成值"/>
        </a-form-item>
        <a-form-item label="备注">
          <a-input v-decorator="['memo', { initialValue: model?.memo }]" placeholder="请输入备注"/>
        </a-form-item>
        <!-- 检查是否有 id 并且大于0，大于0是修改。其他是新增，新增不显示主键ID -->
        <a-form-item v-if="model && model?.id > 0" label="状态">
          <a-select v-decorator="['busiStatus', { initialValue: model?.busiStatus }]" placeholder="请选择">
            <a-select-option value="1">使用中</a-select-option>
            <a-select-option value="2">停用</a-select-option>
          </a-select>
        </a-form-item>
      </a-form>
    </a-spin>
  </a-modal>
</template>

<script>
import pick from 'lodash.pick'
import { getSvSuppliesListUsingGET } from '@/api/api/shouhouguanli.ts'

// 表单字段
const fields = ['name','dedType','dedValue','propValue','supplyId','propType','busiStatus', 'id']

export default {
  props: {
    visible: {
      type: Boolean,
      required: true,
    },
    loading: {
      type: Boolean,
      default: () => false,
    },
    model: {
      type: Object,
      default: () => null,
    },
  },
  data() {
    this.formLayout = {
      labelCol: {
        xs: { span: 24 },
        sm: { span: 7 },
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 13 },
      },
    }
    return {
      dedType: '1',
      form: this.$form.createForm(this),
      optionParam: {
        pageNo: 1,
        pageSize: 999,
        queryCondition: '',
        status: 1,
      },
      supplierOption: []
    }
  },
  mounted() {
    this.getOption()
  },
  methods: {
    getOption() {
      getSvSuppliesListUsingGET(this.optionParam)
        .then((res) => {
          this.supplierOption = res.data.list
        })
        .catch((error) => {
          this.$message.error(((error.response || {}).data || {}).msg || '请求出现错误')
        })
    },
  },
  created() {
    // 防止表单未注册
    fields.forEach((v) => this.form.getFieldDecorator(v))

    // 当 model 发生改变时，为表单设置值
    this.$watch('model', () => {
      this.model && this.form.setFieldsValue(pick(this.model, fields))
    })
  },
}
</script>
